﻿<!DOCTYPE html><html><head><title>Nginx日志限速及地址重写-3</title><meta charset=UTF-8'utf-8'><link href='https://dn-maxiang.qbox.me/res-min/themes/marxico.css' rel='stylesheet'><style></style></head><body><div id='preview-contents' class='note-content'>
                        
                    



<h4 id="鑷姩绱㈠紩鍔熻兘">鑷姩绱㈠紩鍔熻兘</h4>



<pre class="prettyprint hljs-dark"><code class="hljs nginx"><span class="hljs-title">server</span> {<br>        <span class="hljs-title">listen</span>       <span class="hljs-number">8080</span>;<br>        <span class="hljs-title">server_name</span>  localhost;<br>        <span class="hljs-title">root</span> /var/www/web;<br>        <span class="hljs-title">index</span> index.html index.htm;<br><br>        <span class="hljs-title">location</span> /data {<br>            <span class="hljs-title">autoindex</span> <span class="hljs-built_in">on</span>;  <span class="hljs-comment">#鍒楀嚭璇ョ洰褰曚笅鐨勬枃浠?/span&gt;<br>        }<br>    }<br></code></pre>

<blockquote>
  <p>闇€瑕佸湪鐩綍/var/www/web涓嬪缓绔媎ata鐩綍锛岀劧鍚庡湪data鐩綍涓嬫斁鏂囦欢銆?/p&gt;</p>
</blockquote>
<H4 id="nginx-鐘舵€佹鏌?>Nginx 鐘舵€佹鏌?/h4>&#10;&#10;&#10;&#10;<pre class=" 
hljs-dark? prettyprint><CODE class="hljs crmsh"><SPAN 
class=hljs-keyword>location</SPAN> <SPAN class=hljs-title>/nginx_status</SPAN> 
{<BR>stub_status on;<BR>access_log off;<BR>}<BR>璁块棶锛歨ttp://<SPAN 
class=hljs-number>172.16</SPAN>.<SPAN class=hljs-number>130.10</SPAN>:<SPAN 
class=hljs-number>8070</SPAN>/nginx_status<BR></CODE><PRE></PRE>
<H3 id=nginx-鐨?root鎸囦护>Nginx 鐨?root鎸囦护</H3><PRE class="prettyprint hljs-dark"><CODE class="hljs crmsh"><SPAN class=hljs-keyword>location</SPAN> <SPAN class=hljs-title>/img</SPAN>/ {<BR>    alias /var/www/image/;<BR>}<BR><SPAN class=hljs-comment>#鑻ユ寜鐓т笂杩伴厤缃殑璇濓紝鍒欒闂?img/鐩綍閲岄潰鐨勬枃浠舵椂锛宯ingx浼氳嚜鍔ㄥ幓/var/www/image/鐩綍鎵炬枃浠?/span&gt;<BR><SPAN class=hljs-keyword>location</SPAN> <SPAN class=hljs-title>/img</SPAN>/ {<BR>    root /var/www/image;<BR>}<BR><SPAN class=hljs-comment>#鑻ユ寜鐓ц繖绉嶉厤缃殑璇濓紝鍒欒闂?img/鐩綍涓嬬殑鏂囦欢鏃讹紝nginx浼氬幓/var/www/image/img/鐩綍涓嬫壘鏂囦欢</SPAN><BR></CODE></PRE>
<BLOCKQUOTE>
  <P>alias鏄竴涓洰褰曞埆鍚嶇殑瀹氫箟锛宺oot鍒欐槸鏈€涓婂眰鐩綍鐨勫畾涔夈€?/p&gt; </P></BLOCKQUOTE>
<HR>

<h2 id="绗笁鍫傝nginx鐨勬棩蹇楃鐞嗕笌闄愰€? designtimesp=31913>绗笁鍫傝锛歂ginx鐨勬棩蹇楃鐞嗕笌闄愰€?/h2>&#10;&#10;<h5 id=" 鎸囦护?>鎸囦护</h2>



<pre class="prettyprint hljs-dark"><code class="hljs bash">access_<span class="hljs-built_in">log</span><br><span class="hljs-built_in">log</span>_format<br>error_<span class="hljs-built_in">log</span><br></code></pre>

<blockquote>
  <p>闇€瑕佸畨瑁咃細HttpLog妯″潡 ngx_http_log_module <br>
  ./configure 鈥揾elp  鏌ョ湅甯姪鏂囨。</p>
  
  <p>log-format鎸囦护鐢ㄦ潵璁剧疆鏃ュ織鐨勮褰曟牸寮忥紝璇硶濡備笅锛?<br>
  璇硶锛?log_format name format[format鈥 <br>
  浣滅敤鍩燂細 http server <br>
  鍏蜂綋鏃ュ織鏍煎紡濡備笅锛?/p&gt;&lt;
  /p&gt;<
  /p>
</blockquote>



<pre class="prettyprint hljs-dark"><code class="hljs gams">#log_format  main  '<span class="hljs-number">$remote</span>_addr - <span class="hljs-number">$remote</span>_user [<span class="hljs-number">$time</span>_local] "<span class="hljs-number">$request</span>" '<br>#                  '<span class="hljs-number">$status</span> <span class="hljs-number">$body</span>_bytes_sent "<span class="hljs-number">$http</span>_referer" '<br>#                  '"<span class="hljs-number">$http</span>_user_agent" "<span class="hljs-number">$http</span>_x_forwarded_for"';<br><br><span class="hljs-number">$http</span>_x_forwarded_for鍜?span class="hljs-number"&gt;$remote</SPAN>_user鐢ㄤ簬璁板綍ip鍦板潃<br><span class="hljs-number">$remote</span>_user鐢ㄦ潵璁板綍杩滅▼瀹㈡埛绔敤鎴峰悕绉?br&gt;<span class="hljs-number">$time</span>_local鐢ㄦ潵璁板綍璁块棶鏃堕棿鍙婃椂鍖?br&gt;<span class="hljs-number">$request</span>鐢ㄦ潵璁板綍璇锋眰鐨剈rl涓巋ttp鍗忚 <br><span class="hljs-number">$status</span>鐢ㄦ潵璁板綍璇锋眰鐨勭姸鎬侊紝渚嬪鎴愬姛鏃剁姸鎬佷负<span class="hljs-number">200</span>锛岄〉闈㈡壘涓嶅埌鏃朵负<span class="hljs-number">404</span><br><span class="hljs-number">$body</span>_bytes_sent鐢ㄤ簬璁板綍鍙戦€佺粰瀹㈡埛绔殑鏂囦欢涓讳綋鍐呭澶у皬銆?br&gt;<span class="hljs-number">$http</span>_referer鐢ㄤ簬璁板綍鏄粠鍝釜椤甸潰閾炬帴璁块棶杩囨潵鐨?br&gt;<span class="hljs-number">$http</span>_user_agent鐢ㄤ簬璁板綍瀹㈡埛绔祻瑙堝櫒鐨勭浉鍏充俊鎭?br&gt;</code></pre>

<blockquote>
  <p>涓句緥锛?<br>
  172.16.89.18 - - [22/Feb/2017:08:41:43 -0500] 鈥淕ET /data HTTP/1.1鈥?404 58 鈥?鈥?鈥淢ozilla/4.0  <br>
  (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727;  <br>
  .NET
  CLR 3.5.30729; 
  .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)鈥?/p&gt;&lt; 
  /p&gt;<
             /p>
</blockquote>

<p>鎵惧嚭logs鐩綍涓媋ccess.log婕旂ず锛屽仛涓€娆￠〉闈㈣闂紨绀恒€?/p&gt; 
<H5 
id="鍋氫竴娆′笉鍚宻erver鐨勬棩蹇楄闂紨绀?>鍋氫竴娆′笉鍚宻erver鐨勬棩蹇楄闂紨绀?/h5>&#10;&#10;&#10;&#10;<pre class=" 
hljs-dark? prettyprint><CODE class="hljs gams">#log_format main '<SPAN 
class=hljs-number>$remote</SPAN>_addr - <SPAN 
class=hljs-number>$remote</SPAN>_user [<SPAN 
class=hljs-number>$time</SPAN>_local] "

<span class="hljs-number">



                $request</span>" '<br>#                  '<span class="hljs-number">$status</span> <span class="hljs-number">$body</span>_bytes_sent "<span class="hljs-number">$http</span>_referer" '<br>#                  '"<span class="hljs-number">$http</span>_user_agent" "<span class="hljs-number">$http</span>_x_forw<br><br>#log_format  slave  '<span class="hljs-number">$remote</span>_addr - <span class="hljs-number">$remote</span>_user [<span class="hljs-number">$time</span>_local] "<span class="hljs-number">$request</span>" <br></CODE><PRE></PRE>

<p><strong>璇硶</strong>锛?access_log path [format [buffer=size | off ] 榛樿鍊硷細 access_log log/access.log combined <br>
<strong>浣滅敤鍩?/strong&gt;锛?http, server, location</p>

<blockquote>
  <p>鎸囦护 access_log 鎸囨淳璺緞銆佹牸寮忓拰缂撳瓨澶у皬銆傚弬鏁?鈥渙ff鈥?灏嗘竻闄ゅ綋鍓嶇骇鍒殑鎵€鏈?access_log 
  鎸囦护銆?/p&gt;&lt; /p&gt;<   /p>
</blockquote>



<pre class="prettyprint hljs-dark"><code class="hljs stata">鍦?access_log  logs/host.access.<span class="hljs-keyword">log</span>  slave;     杩欎釜璺熷悕绉?br&gt;<br>error_log /<span class="hljs-keyword">var</span>/<span class="hljs-keyword">log</span>/nginx/<span class="hljs-keyword">error</span>/hostname-<span class="hljs-keyword">error</span>.<span class="hljs-keyword">log</span> warn;  杩欎釜璺熺骇鍒?br&gt;</code></pre>



<h4 id="瀹氫箟鏃ュ織">瀹氫箟鏃ュ織</h4>

<blockquote>
  <p>acces_log /data/log/$server_name.log main buffer=32k; <br>
  (鑷?.7.4涔嬪悗nginx鏀寔鍙橀噺) <br>
  buffer=32k 锛?琛ㄧず璁剧疆鍐呭瓨缂撳啿鍖虹殑澶у皬 <br>
  鏂逛究锛屽綋楂樺苟鍙戣闂椂锛岀敤鍙橀噺鑰楄祫婧愩€?娴嬭瘯鐜鍙互鎼烇紝鐢熶骇鐜涓嶆悶銆?/p&gt;&lt; 
  /p&gt;<
  /p>
</blockquote>
<H4 id="nginx寮€鍚帇缂╄緭鍑?>Nginx寮€鍚帇缂╄緭鍑?/h4>&#10;&#10;&#10;&#10;<pre class=" 
hljs-dark? prettyprint><CODE class="hljs nginx"><SPAN 
class=hljs-title>gzip</SPAN> <SPAN class=hljs-built_in>on</SPAN>;<BR><SPAN 
class=hljs-title>gzip_min_length</SPAN> <SPAN 
class=hljs-number>1k</SPAN>;<BR><SPAN class=hljs-title>gzip_buffer</SPAN> <SPAN 
class=hljs-number>4</SPAN> <SPAN class=hljs-number>16k</SPAN>;<BR><SPAN 
class=hljs-title>gzip_http_version</SPAN> <SPAN class=hljs-number>1</SPAN>.<SPAN 
class=hljs-number>1</SPAN>;<BR><SPAN class=hljs-title>gzip_comp_level</SPAN> 
<SPAN class=hljs-number>2</SPAN>;<BR><SPAN class=hljs-title>gzip_type</SPAN> 
text/plain application/x-javascript text/css application/xml;<BR><SPAN 
class=hljs-title>gzip_vary</SPAN> <SPAN 
class=hljs-built_in>on</SPAN>;<BR></CODE><PRE></PRE>
<BLOCKQUOTE>
  <P>杩欐牱鍦ㄦ暟鎹紶杈撶殑杩囩▼涓氨鏄帇缂╃殑銆?/p&gt; </P></BLOCKQUOTE>



<h4 id="nginx鐨勭紦瀛? designtimesp=32000>Nginx鐨勭紦瀛?/h4>&#10;&#10;&#10;&#10;<pre class=" hljs-dark? prettyprint><code class="hljs gherkin">location ~.<span class="hljs-keyword">*</span>\.(gif|<span class="hljs-string">jpg</span>|<span class="hljs-string">jpge</span>|<span class="hljs-string">png</span>|<span class="hljs-string">bmp</span>|<span class="hljs-string">swf)$<br>{<br>    expires 30d;<br>}<br>location ~.*\.(js</span>|<span class="hljs-string">css)?$<br>{<br>    expires 1h;<br>}</span><br></code><PRE></PRE>

<blockquote>
  <p>闈欐€佺殑鏃堕棿闀匡紝鍔ㄦ€佺殑鏃堕棿鐭€備笌婧愮殑鍙樺寲銆?/p&gt;</p>
</blockquote>
<H4 
id="璁惧畾闄愰€?>璁惧畾闄愰€?/h4>&#10;&#10;<p>瀵逛簬娴佹挱鏀惧櫒鏉ヨ鏄渶閲嶈鐨勩€?/p>&#10;&#10;<p>鐢ㄧ櫨搴﹂檺閫熷湡璞嗗拰鐖卞鑹鸿璧凤紝姣斿鐧惧害闄愬害鍦熻眴锛屼笉闄恑qiyi杩欐牱锛屽鎴锋槸涓嶆槸鏈€鍠滄iqiyi銆?/p>&#10;&#10;&#10;&#10;<pre class=" 
hljs-dark? prettyprint><CODE class="hljs xquery">location 
/download<BR>{<BR>limit_rate <SPAN class=hljs-number>256</SPAN>k;<BR>proxy_pass 
http://<SPAN class=hljs-number>1.2</SPAN>.<SPAN 
class=hljs-number>3.4</SPAN>;<BR>proxy_set_header Host <SPAN 
class=hljs-variable>$host</SPAN>;<BR>proxy_set_header X-Forwarded-For <SPAN 
class=hljs-variable>$remote</SPAN>_addr;<BR>}<BR>location 
/movie<BR>{<BR>limit_rate_after <SPAN 
class=hljs-number>10</SPAN>m;<BR>limit_rate <SPAN 
class=hljs-number>100</SPAN>k;<BR>}<BR></CODE><PRE></PRE>
<BLOCKQUOTE>
  <P>缂撳啿10M涓嶉檺閫燂紝褰?0M杩囧悗锛?00k/s</P></BLOCKQUOTE><PRE class="prettyprint hljs-dark"><CODE class="hljs xquery"><SPAN class=hljs-keyword>if</SPAN>(<SPAN class=hljs-variable>$http</SPAN>_user_agent ~ Google|Yahoo|baidu)<BR>{<BR>    limit_rate <SPAN class=hljs-number>20</SPAN>k;<BR>}<BR></CODE></PRE>
<BLOCKQUOTE>
  <P>濡傛灉鐢ㄦ埛鐧诲綍杩嘒oogle|Yahoo|baidu杩囨潵鐨勶紝闄愰€?0k/s <BR>娴嬭瘯鏄惁闄愰€燂細wget 

<a target="_blank"







 href="http://192.168.1.20/data/test" >                                           


  




              


   
     http://192.168.1.20/data/test</a> <br>
  杩呴浄灏辨槸杩欎箞鎼炵殑</P></BLOCKQUOTE>

<h2 id="绗洓鍫傝nginx鐨勫湴鍧€閲嶅啓-rewrite">绗洓鍫傝锛歂ginx鐨勫湴鍧€閲嶅啓 rewrite</h2>

<p>rewrite涓昏鐨勫姛鑳藉氨鏄疄鐜皍rl鐨勯噸鍐欙紝Nginx鐨剅ewrite瑙勫垯閲囩敤PCRE <br>
(Perl Compatible Regular Expressions)Perl鍏煎姝ｅ垯琛ㄨ揪寮忕殑璇硶杩涜 <br>
瑙勫垯鍖归厤锛屽鏋滈渶瑕丯ginx鐨剅ewrite鍔熻兘锛屽湪缂栬瘧Nginx涔嬪墠锛岄渶瑕佺紪璇戝畨瑁?<br>
PCRE搴撱€?<br>
PCRE鐢ㄤ簬鏀粯姝ｅ垯琛ㄨ揪寮忋€?/p&gt;

<blockquote>
  <p>nginx閫氳繃ngx_http_rewrite_module妯″潡鏀寔url閲嶅啓銆佹敮鎸乮f鏉′欢鍒ゆ柇锛屼絾涓嶆敮鎸乪lse銆?/p&gt;</p>
</blockquote>

<p><strong>nginx rewrite鎸囦护鎵ц椤哄簭锛?/strong&gt; <br>
1.鎵цserver鍧楃殑rewrite鎸囦护(杩欓噷鐨勫潡鎸囩殑鏄痵erver鍏抽敭瀛楀悗{}鍖呭洿鐨勫尯鍩燂紝鍏跺畠xx鍧楃被浼? <br>
2.鎵цlocation鍖归厤 <br>
3.鎵ц閫夊畾鐨刲ocation涓殑rewrite鎸囦护 <br>
濡傛灉鍏朵腑鏌愭URI琚噸鍐欙紝鍒欓噸鏂板惊鐜墽琛?-3锛岀洿鍒版壘鍒扮湡瀹炲瓨鍦ㄧ殑鏂囦欢</p>

<p>濡傛灉寰幆瓒呰繃10娆★紝鍒欒繑鍥?00 Internal Server Error閿欒</p>



<h4 id="break鎸囦护">break鎸囦护</h4>

<blockquote>
  <p>璇硶锛歜reak; <br>
  榛樿鍊硷細鏃?<br>
  浣滅敤鍩燂細server,location,if</p>
</blockquote>

<p>鍋滄鎵ц褰撳墠铏氭嫙涓绘満鐨勫悗缁璻ewrite鎸囦护闆?<br>
break鎸囦护瀹炰緥锛?/p&gt;



<pre class="prettyprint hljs-dark"><code class="hljs xquery"> <span class="hljs-keyword">if</span> (<span class="hljs-variable">$slow</span>) {<br>     limit_rate <span class="hljs-number">10</span>k;<br>     break;<br><br></code></pre>



<h4 id="if鎸囦护">if鎸囦护</h4>

<blockquote>
  <p>璇硶锛歩f(condition){鈥 <br>
  榛樿鍊硷細鏃?<br>
  浣滅敤鍩燂細server,location <br>
  瀵圭粰瀹氱殑鏉′欢condition杩涜鍒ゆ柇銆傚鏋滀负鐪燂紝澶ф嫭鍙峰唴鐨剅ewrite鎸囦护灏嗚鎵ц銆?/p&gt;&lt; 
  /p&gt;<
  /p>
</blockquote>

<p>if鏉′欢(conditon)鍙互鏄涓嬩换浣曞唴瀹?</p>

<blockquote>
  <p>涓€涓彉閲忓悕锛沠alse濡傛灉杩欎釜鍙橀噺鏄┖瀛楃涓叉垨鑰呬互0寮€濮嬬殑瀛楃涓诧紱 <br>
  浣跨敤= ,!= 姣旇緝鐨勪竴涓彉閲忓拰瀛楃涓?<br>
  鏄敤~锛?~*涓庢鍒欒〃杈惧紡鍖归厤鐨勫彉閲忥紝濡傛灉杩欎釜姝ｅ垯琛ㄨ揪寮忎腑鍖呭惈}锛?鍒欐暣涓〃杈惧紡闇€瑕佺敤 鍙屽紩鍙锋垨鍗曞紩鍙?鍖呭洿 <br>
  浣跨敤-f 锛?-f 妫€鏌ヤ竴涓枃浠舵槸鍚﹀瓨鍦?<br>
  浣跨敤-d, !-d 妫€鏌ヤ竴涓洰褰曟槸鍚﹀瓨鍦?<br>
  浣跨敤-e 锛?-e 妫€鏌ヤ竴涓枃浠躲€佺洰褰曘€佺鍙烽摼鎺ユ槸鍚﹀瓨鍦?<br>
  浣跨敤-x 锛?!-x 妫€鏌ヤ竴涓枃浠舵槸鍚﹀彲鎵ц</p>
</blockquote>



<pre class="prettyprint hljs-dark"><code class="hljs xquery"><span class="hljs-keyword">if</span> (<span class="hljs-variable">$http</span>_user_agent ~ MSIE) {<br>     rewrite ^(.*)$ /msie/<span class="hljs-variable">$1</span> break;<br> }<br> <span class="hljs-keyword">if</span> (<span class="hljs-variable">$request</span>_method = POST) {<br>     return <span class="hljs-number">405</span>;<br> }<br><br> <span class="hljs-keyword">if</span> (<span class="hljs-variable">$slow</span>) {<br>     limit_rate <span class="hljs-number">10</span>k;<br> }<br><br> <span class="hljs-keyword">if</span> (<span class="hljs-variable">$invalid</span>_referer) {<br>     return <span class="hljs-number">403</span>;<br> }<br></code></pre>



<h4 id="return鎸囦护">return鎸囦护</h4>

<blockquote>
  <p>return鎸囦护 <br>
  璇硶锛?<br>
  return code; <br>
  return code URL; <br>
  榛樿鍊硷細鏃?<br>
  浣滅敤鍩燂細server,location,if</p>
</blockquote>

<p>鍋滄澶勭悊骞惰繑鍥炴寚瀹氱姸鎬佺爜(code)缁欏鎴风銆?/p&gt;



<h4 id="rewrite鎸囦护">rewrite鎸囦护</h4>

<blockquote>
  <p>rewrite瑙勫垯缂栧啓瀹炰緥</p>
</blockquote>

<ul><li><strong>灏嗗師鏉ヨ璁块棶/data鐩綍閲嶅啓涓?bbs</strong></li>
</ul>



<pre class="prettyprint hljs-dark"><code class="hljs nginx"><span class="hljs-title">location</span> /data {<br>            <span class="hljs-title">rewrite</span><span class="hljs-regexp"> ^/data/?$</span> /bbs/ <span class="hljs-built_in">permanent</span>;<br>        }<br></code></pre>

<p><strong>婕旂ず</strong></p>

<ul><li><strong>鏍规嵁涓嶅悓鐨勬祻瑙堝櫒灏嗗緱鍒颁笉鍚岀殑缁撴灉</strong></li>
</ul>



<pre class="prettyprint hljs-dark"><code class="hljs xquery"><span class="hljs-keyword">if</span>(<span class="hljs-variable">$http</span>_user_agent ~MSIE){<br>    rewrite ^(.*)$ /msie/<span class="hljs-variable">$1</span> break;<br>}<br></code></pre>

<ul><li><strong>闃叉鐩楅摼</strong></li>
</ul>



<pre class="prettyprint hljs-dark"><code class="hljs xquery">location ~* \.(gif|jpg|png|swf|flv)${<br>    valid_referers none blocked www.test.com *.test.com;<br>    if(<span class="hljs-variable">$invalid</span>_referer){<br>        rewrite ^/(.*) http://www.test.com/block.html<br>    }<br>}<br></code></pre>

<blockquote>
  <p>鍙湁閫氳繃test.com缃戠珯璁块棶杩囨潵鐨勫浘鐗囨墠姝ｅ父鏄剧ず锛?<br>
  鍚﹀垯閲嶅啓鍒?a href="http://www.test.com/block.html" 
  target="_blank"&gt;http://www.test.com/block.html</A></p>
</blockquote>

<p>鐩楅摼鏄寚鏈嶅姟鎻愪緵鍟嗚嚜宸变笉鎻愪緵鏈嶅姟鐨勫唴瀹癸紝閫氳繃鎶€鏈墜娈电粫杩囧叾瀹冩湁鍒╃泭鐨?<br>
鏈€缁堢敤鎴风晫闈紙濡傚箍鍛婏級锛岀洿鎺ュ湪鑷繁鐨勭綉绔欎笂鍚戞渶缁堢敤鎴锋彁渚涘叾瀹冩湇鍔℃彁渚涘晢 <br>
鐨勬湇鍔″唴瀹癸紝楠楀彇鏈€缁堢敤鎴风殑娴忚鍜岀偣鍑荤巼銆傚彈鐩婅€呬笉鎻愪緵璧勬簮鎴栨彁渚涘緢灏戠殑璧勬簮锛?<br>
鑰岀湡姝ｇ殑鏈嶅姟鎻愪緵鍟嗗嵈寰椾笉鍒颁换浣曠殑鏀剁泭銆?/p&gt;

<p>缃戠珯鐩楅摼浼氬ぇ閲忔秷鑰楄鐩楅摼缃戠珯鐨勫甫瀹斤紝鑰岀湡姝ｇ殑鐐瑰嚮鐜囦篃璁镐細寰堝皬锛?<br>
涓ラ噸鎹熷浜嗚鐩楅摼缃戠珯鐨勫埄鐩娿€?<br>
甯歌鐨勭洍閾炬湁浠ヤ笅鍑犵锛氬浘鐗囩洍閾俱€侀煶棰戠洍閾俱€佽棰戠洍閾俱€佹枃浠剁洍閾俱€?/p&gt;

<p></p><ul><li><strong>瀹炵幇鍩熷悕璺宠浆</strong> <br></li></ul><p></p>

<blockquote>
  鎵€鏈夊www.abc.com鐨勮闂紝redirect鍒皐ww.test.com
  
  </blockquote>

<pre class="prettyprint hljs-dark"><code class="hljs nginx"><span class="hljs-title">server</span><br>    {<br>    <span class="hljs-title">listen</span> <span class="hljs-number">80</span>;<br>    <span class="hljs-title">server_name</span> www.test.com;<br>    <span class="hljs-title">index</span> index.html index.php;<br>    <span class="hljs-title">root</span> /export/home/www<br>    if(<span class="hljs-variable">$host</span>=<span class="hljs-string">"www.abc.com"</span>){<br>        <span class="hljs-title">rewrite</span><span class="hljs-regexp"> ^/(.*)$</span> http//www.test.com/$ <span class="hljs-built_in">permanent</span>;<br>        }<br>    }<br></code></pre>

<blockquote>
  <p>閮ㄥ垎鍏ㄥ眬鍙橀噺</p>
</blockquote>

<pre class="prettyprint hljs-dark"><code class="hljs xquery"><span class="hljs-variable">$args</span>  <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   璇锋眰涓殑鍙傛暟;<br><span class="hljs-variable">$binary</span>_remote_addr    <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   杩滅▼鍦板潃鐨勪簩杩涘埗琛ㄧず<br><span class="hljs-variable">$body</span>_bytes_sent   <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   宸插彂閫佺殑娑堟伅浣撳瓧鑺傛暟<br><span class="hljs-variable">$content</span>_length    <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   HTTP璇锋眰淇℃伅閲岀殑<span class="hljs-string">"Content-Length"</span>;<br><span class="hljs-variable">$content</span>_type  <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   璇锋眰淇℃伅閲岀殑<span class="hljs-string">"Content-Type"</span>;<br><span class="hljs-variable">$document</span>_root <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   閽堝褰撳墠璇锋眰鐨勬牴璺緞璁剧疆鍊?<br><span class="hljs-variable">$document</span>_uri  <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   涓?span class="hljs-variable"&gt;$uri</SPAN>鐩稿悓;<br><span class="hljs-variable">$host</span>  <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   璇锋眰淇℃伅涓殑<span class="hljs-string">"Host"</span>锛屽鏋滆姹備腑娌℃湁Host琛岋紝鍒欑瓑浜庤缃殑鏈嶅姟鍣ㄥ悕;<br><span class="hljs-variable">$hostname</span>  <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   <br><span class="hljs-variable">$http</span>_cookie   <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   cookie 淇℃伅<br><span class="hljs-variable">$http</span>_post <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   <br><span class="hljs-variable">$http</span>_referer  <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   寮曠敤鍦板潃<br><span class="hljs-variable">$http</span>_user_agent   <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   瀹㈡埛绔唬鐞嗕俊鎭?br&gt;<span class="hljs-variable">$http</span>_via  <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   鏈€鍚庝竴涓闂湇鍔″櫒鐨処p鍦板潃銆俬ttp://www.cnblogs.com/deng02/archive/<span class="hljs-number">2009</span>/<span class="hljs-number">02</span>/<span class="hljs-number">11</span>/<span class="hljs-number">1387911</span>.html<br><span class="hljs-variable">$http</span>_x_forwarded_for  <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   鐩稿綋浜庣綉缁滆闂矾寰勩€俬ttp://www.cnblogs.com/craig/archive/<span class="hljs-number">2008</span>/<span class="hljs-number">11</span>/<span class="hljs-number">18</span>/<span class="hljs-number">1335809</span>.html<br><span class="hljs-variable">$is</span>_args   <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   <br><span class="hljs-variable">$limit</span>_rate    <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   瀵硅繛鎺ラ€熺巼鐨勯檺鍒?<br><span class="hljs-variable">$nginx</span>_version <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   <br><span class="hljs-variable">$pid</span>   <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   <br><span class="hljs-variable">$query</span>_string  <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   涓?span class="hljs-variable"&gt;$args</SPAN>鐩稿悓;<br><span class="hljs-variable">$realpath</span>_root <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   <br><span class="hljs-variable">$remote</span>_addr   <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   瀹㈡埛绔湴鍧€;<br><span class="hljs-variable">$remote</span>_port   <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   瀹㈡埛绔鍙ｅ彿;<br><span class="hljs-variable">$remote</span>_user   <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   瀹㈡埛绔敤鎴峰悕锛岃璇佺敤;<br><span class="hljs-variable">$request</span>   <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   鐢ㄦ埛璇锋眰<br><span class="hljs-variable">$request</span>_body  <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   <br><span class="hljs-variable">$request</span>_body_file <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   鍙戝線鍚庣鐨勬湰鍦版枃浠跺悕绉?br&gt;<span class="hljs-variable">$request</span>_completion    <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   <br><span class="hljs-variable">$request</span>_filename  <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   褰撳墠璇锋眰鐨勬枃浠惰矾寰勫悕<br><span class="hljs-variable">$request</span>_method    <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   璇锋眰鐨勬柟娉曪紝姣斿<span class="hljs-string">"GET"</span>銆?span class="hljs-string"&gt;"POST"</SPAN>绛?<br><span class="hljs-variable">$request</span>_uri   <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   璇锋眰鐨刄RI锛屽甫鍙傛暟;<br><span class="hljs-variable">$scheme</span>    <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   鎵€鐢ㄧ殑鍗忚锛屾瘮濡俬ttp鎴栬€呮槸https锛屾瘮濡俽ewrite^(.+)$<span class="hljs-variable">$scheme</span>://example.com<span class="hljs-variable">$1redirect</span>;<br><span class="hljs-variable">$sent</span>_http_cache_control   <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   <br><span class="hljs-variable">$sent</span>_http_connection  <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   <br><span class="hljs-variable">$sent</span>_http_content_length  <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   <br><span class="hljs-variable">$sent</span>_http_content_type    <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   <br><span class="hljs-variable">$sent</span>_http_keep_alive  <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   <br><span class="hljs-variable">$sent</span>_http_last_modified   <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   <br><span class="hljs-variable">$sent</span>_http_location    <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   <br><span class="hljs-variable">$sent</span>_http_transfer_encoding   <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   <br><span class="hljs-variable">$server</span>_addr   <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   鏈嶅姟鍣ㄥ湴鍧€锛屽鏋滄病鏈夌敤listen鎸囨槑鏈嶅姟鍣ㄥ湴鍧€锛屼娇鐢ㄨ繖涓彉閲忓皢鍙戣捣涓€娆＄郴缁熻皟鐢ㄤ互鍙栧緱鍦板潃(閫犳垚璧勬簮娴垂);<br><span class="hljs-variable">$server</span>_name   <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   璇锋眰鍒拌揪鐨勬湇鍔″櫒鍚?<br><span class="hljs-variable">$server</span>_port   <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   璇锋眰鍒拌揪鐨勬湇鍔″櫒绔彛鍙?<br><span class="hljs-variable">$server</span>_protocol   <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   璇锋眰鐨勫崗璁増鏈紝<span class="hljs-string">"HTTP/1.0"</span>鎴?span class="hljs-string"&gt;"HTTP/1.1"</SPAN>;<br><span class="hljs-variable">$uri</span>   <span class="hljs-number">1.0</span>.<span class="hljs-number">8</span>   璇锋眰鐨刄RI锛屽彲鑳藉拰鏈€鍒濈殑鍊兼湁涓嶅悓锛屾瘮濡傜粡杩囬噸瀹氬悜涔嬬被鐨勩€?br&gt;<br></code></pre></div></STRONG></H4></H4></H4></STRONG></H5></H4></body></html>